package com.gxks.lhs.controller.user;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxks.lhs.constants.R;
import com.gxks.lhs.entity.Category;
import com.gxks.lhs.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 分类表 前端控制器
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-19
 */

@Api(tags = "商品分类模块")
@Slf4j
@RestController
@RequestMapping("/restful-api/category")
public class UserCategoryController {

    @Autowired
    private ICategoryService categoryService;

    @ApiOperation("添加商品分类")
    @PostMapping("/add")
    public R addCategory(@RequestBody Category category) {
        boolean b = categoryService.addCategory(category);
        if (b) {
            return R.success();
        }
        return R.error();
    }

    @ApiOperation("更新商品分类")
    @PutMapping
    public R updateCategory(@RequestBody Category category) {
        boolean b = categoryService.updateCategory(category);
        if (b) {
            return R.success();
        }
        return R.error();
    }

    @ApiOperation("删除商品分类")
    @DeleteMapping("/{id}")
    public R deleteCategory(@PathVariable Integer id) {
        boolean b = categoryService.deleteCategory(id);
        if (b) {
            return R.success();
        }
        return R.error();
    }

    @ApiOperation("根据id查找商品分类的信息")
    @GetMapping("/{id}")
    public R getCategoryById(@PathVariable Integer id) {
        Category category = categoryService.findCategoryById(id);
        if (category == null) {
            return R.error();
        }
        return R.success(category);
    }

    @ApiOperation("获取商品分类列表")
    @GetMapping("/list")
    public R getCategoryList(@RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                             @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Page<Category> allCategory = categoryService.findAllCategoryByUser(current, size);
        if (allCategory.getTotal() <= 0) {
            return R.error("暂无数据");
        }
        return R.success(allCategory, "查询成功");
    }

    @ApiOperation("根据商品分类名称模糊查所商品分类的信息")
    @GetMapping
    public R getCategoryByName(@RequestParam String name,
                               @RequestParam(value = "current", required = false, defaultValue = "1") Integer current,
                               @RequestParam(value = "size", required = false, defaultValue = "10") Integer size) {
        Page<Category> page = categoryService.findCategoryByName(name, current, size);
        if (page.getTotal() <= 0) {
            return R.error("暂无数据");
        }
        return R.success(page, "查询成功");
    }

    @ApiOperation("获取所有二手手机的的基本分类信息")
    @GetMapping("/phone")
    public R getPhoneCategory() {
        return R.success(categoryService.findCategoryName());
    }
}

